package com.h3c.ptability.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eos.common.util.IdWorker;
import com.h3c.ptability.dto.electronicSign.PtOrgInfoDTO;
import com.h3c.ptability.entity.PtOrgInfo;
import com.h3c.ptability.mapstruct.PtOrgInfoMapstruct;
import com.h3c.ptability.service.IPtOrgInfoService;
import com.h3c.ptability.support.ResultData;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * 控制层
 *
 * @author chenyifan
 * @since 2022-05-24 09:34:58
 */
@CrossOrigin
@RestController
@AllArgsConstructor
@RequestMapping("/ptOrgInfo")
public class PtOrgInfoController {
    /**
     * 服务对象
     */
    private final IPtOrgInfoService ptOrgInfoService;

    /**
     * 分页回显配置
     *
     * @param orgInfoDTO
     * @return
     */
    @PostMapping("/pageOrgInfo")
    public ResultData<IPage<PtOrgInfo>> pageOrgInfo(@RequestBody PtOrgInfoDTO orgInfoDTO) {
        LambdaQueryWrapper<PtOrgInfo> queryWrapper = new QueryWrapper<PtOrgInfo>().lambda();
        if(StringUtils.isNotBlank(orgInfoDTO.getOrgName())){
            queryWrapper.like(PtOrgInfo::getOrgName,orgInfoDTO.getOrgName());
        }
        if(StringUtils.isNotBlank(orgInfoDTO.getSapCode())){
            queryWrapper.like(PtOrgInfo::getSapScope,orgInfoDTO.getSapCode());
        }
        Page<PtOrgInfo> page = ptOrgInfoService.page(new Page<>(orgInfoDTO.getQuery().getPageNum(), orgInfoDTO.getQuery().getPageSize()), queryWrapper);
        return ResultData.data(page);
    }

    /**
     * 添加配置
     *
     * @param orgInfoDTO
     * @return
     */
    @PostMapping("/saveOrgInfo")
    public ResultData<String> saveOrgInfo(@RequestBody PtOrgInfoDTO orgInfoDTO) {
        PtOrgInfo ptOrgInfo = PtOrgInfoMapstruct.INSTANCE.dtoToEntity(orgInfoDTO);
        ptOrgInfoService.save(ptOrgInfo.setId(IdWorker.nextId() + ""));
        return ResultData.success("添加成功");
    }

    /**
     * 更新配置
     *
     * @param orgInfoDTO
     * @return
     */
    @PostMapping("/updateOrgInfo")
    public ResultData<String> updateOrgInfo(@RequestBody PtOrgInfoDTO orgInfoDTO) {
        PtOrgInfo ptOrgInfo = PtOrgInfoMapstruct.INSTANCE.dtoToEntity(orgInfoDTO);
        ptOrgInfoService.updateById(ptOrgInfo);
        return ResultData.success("更新成功");
    }

    /**
     * 删除配置
     * @param id
     * @return
     */
    @GetMapping("deleteOrgInfo")
    public ResultData<String> deleteOrgInfo(@RequestParam String id) {
        ptOrgInfoService.removeById(id);
        return ResultData.success("删除成功");
    }
}

